package com.mccken.annotation.logoperator.parse;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.context.expression.AnnotatedElementKey;
import org.springframework.context.expression.CachedExpressionEvaluator;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-01-04 17:20
 **/
public class LogRecordExpressionEvaluator extends CachedExpressionEvaluator {
	
	// 为了缓存方法，、表达式和SpEL的Expression的对应关系，让方法注解上添加的SpEl表达式只解析一次
	private Map<ExpressionKey, Expression> expressionCache = new ConcurrentHashMap<>(64);
	
	// 为了缓存传入到Expression表达式的Object 
	private final Map<AnnotatedElementKey, Method> targetMethodCache = new ConcurrentHashMap<>(64);
	
	public String parseExpression(String conditionExpression, AnnotatedElementKey methodKey, EvaluationContext evalContext) {
		// getExpression 会从expressionCache中获取到@LogRecordAnnotation注解上的表达式的解析Expression的实例
		// getValue 传入给evalContext，类似root的子值
		return getExpression(this.expressionCache,methodKey, conditionExpression).getValue(evalContext, String.class);
	}
	
	
}
